package com.leyou.item.controller;


import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpecParamService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.security.PrivateKey;
import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private SkuService skuService;
    @Autowired
    private SpuService spuService;
    @Autowired
    private SpuDetailService spuDetailService;

//    商品分页查询 测试请求地址：http://localhost:8081/goods/spu/page?saleable=true&page=2&rows=5
    @GetMapping("/spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> querySpuByPage(@RequestParam(value = "page",defaultValue = "1") Integer page,
                                                          @RequestParam(value = "rows",defaultValue = "5")Integer rows,
                                                          @RequestParam(value = "saleable",required = false) Boolean saleable,
                                                          @RequestParam(value = "categoryId",required = false)Long categoryId,
                                                          @RequestParam(value = "brandId",required = false)Long brandId,
                                                          @RequestParam(value = "id",required = false)Long id){
        return ResponseEntity.ok(spuService.querySpuByPage(page, rows, saleable, categoryId, brandId, id));
    }
//新增商品  @param spuDTO 页面提交商品信息
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){

        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
//修改商品上下架
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSpuSaleable(@RequestParam("id") Long id,
                                                  @RequestParam("saleable") Boolean saleable){
        spuService.updateSabeable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
//    根据id查询spu及sku、spuDetail等
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> queryGoodsById(@PathVariable("id") Long id){

        return ResponseEntity.ok(spuService.queryGoodsById(id));
    }
//    更新商品
    @PutMapping
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO){
        spuService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
//根据spuID查询sku的集合
    @GetMapping("/sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySpecBySpuId(@RequestParam("id") Long id){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.query().eq("spu_id", id).list()));
    }
//根据id集合查询SKU集合
    @GetMapping("/sku/list")
    public ResponseEntity<List<SkuDTO>> querySkuListByIds(@RequestParam("ids") List<Long> ids){
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuService.list()));
    }
//    根据spuId查询spu的所有规格参数值
    @GetMapping("/spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(@RequestParam("id")Long id,
                                                             @RequestParam(value = "searching",required = false)
                                                                     Boolean searching){
        return ResponseEntity.ok(spuDetailService.queryParamValues(id,searching));
    }
//    根据id查询商品
    @GetMapping("/spu/{id}")
    public ResponseEntity<SpuDTO> queryById(@PathVariable("id") Long id){

        return ResponseEntity.ok(new SpuDTO(spuService.getById(id)));
    }


    /**
     * 根据spu的Id查询商品详情
     *
     * @param spuId spuId
     * @return 商品详情
     */
    @GetMapping("/spu/detail")
    public ResponseEntity<SpuDetailDTO> queryDetailBySpuId(@RequestParam("id") Long spuId) {
        return ResponseEntity.ok(spuDetailService.queryDetailBySpuId(spuId));
    }
}
